package com.hm.PushCaseService.framework.service.base;

import com.alibaba.fastjson2.JSON;
import com.hm.PushCaseService.framework.response.ResponseBean;
import com.hm.PushCaseService.framework.service.crypto.ICryptoService;
import com.hm.PushCaseService.framework.service.session.ISessionService;
import com.hm.PushCaseService.utils.json.JsonUtils;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 处理post请求帮助类
 */

@Service
public class BaseService<T> implements IBaseService<T> {

    //加密解密服务
    private final ICryptoService iCryptoService;

    //session服务
    private final ISessionService iSessionService;

    public BaseService(ICryptoService iCryptoService, ISessionService iSessionService) {
        this.iCryptoService = iCryptoService;
        this.iSessionService = iSessionService;
    }

    @Override
    public HashMap<String, String> handleRequest(HttpServletRequest request) {
        HashMap<String, String> resMap = new HashMap<>();
        try {
            String sessionId = request.getHeader("sessionId");
            String body = request.getReader().readLine();
            //校验session
            if (sessionId == null || sessionId.isEmpty()) {
                resMap.put("state", "001");
                resMap.put("msg", "sessionId为空");
                return resMap;
            }
            //校验body
            if (body == null || body.isEmpty()) {
                resMap.put("state", "002");
                resMap.put("msg", "body为空");
                return resMap;
            }
            //校验authorization的合法性->session
            HashMap<String, String> verifierMap = iSessionService.verifier(sessionId);
            String state = verifierMap.get("state");
            String msg = verifierMap.get("msg");
            String secretKey = verifierMap.get("secretKey");
            if (!state.equals("000")) {
                resMap.put("state", "003");
                resMap.put("msg", msg);
                return resMap;
            }
            //通过sessionId获取秘钥->解析body
            String realBody = iCryptoService.decrypt(secretKey, body);
            boolean json = JsonUtils.isJson(realBody);
            if (!json) {
                resMap.put("state", "004");
                resMap.put("msg", "body非json格式");
                return resMap;
            }
            //解密成功
            resMap.put("state", "000");
            resMap.put("msg", "body非json格式");
            resMap.put("secretKey", secretKey);
            resMap.put("body", realBody);
        } catch (Exception e) {
            resMap.put("state", "005");
            resMap.put("msg", "请求失败：" + e.getMessage());
        }
        return resMap;
    }

    @Override
    public String generateSucceed(String realSecretKey, T data) {
        ResponseBean<T> resultBean = new ResponseBean<>();
        resultBean.code = 1000;
        resultBean.des = "查询成功";
        resultBean.data = data;
        String resultStr = JSON.toJSONString(resultBean);
        return iCryptoService.encrypt(realSecretKey, resultStr);
    }

    @Override
    public String generateSucceed(String realSecretKey, int code, T data) {
        ResponseBean<T> resultBean = new ResponseBean<>();
        resultBean.code = code;
        resultBean.des = "查询成功";
        resultBean.data = data;
        String resultStr = JSON.toJSONString(resultBean);
        return iCryptoService.encrypt(realSecretKey, resultStr);
    }

    @Override
    public String generateFail(String des) {
        ResponseBean<T> resultBean = new ResponseBean<>();
        resultBean.code = 2000;
        resultBean.des = des;
        return JSON.toJSONString(resultBean);
    }

    @Override
    public String generateFail(int code, String des) {
        ResponseBean<T> resultBean = new ResponseBean<>();
        resultBean.code = code;
        resultBean.des = des;
        return JSON.toJSONString(resultBean);
    }

}
